<?php
class OnlineUserForm extends Form{
	function OnlineUserForm(){
		if(User::is_az_team() && DEBUG){
			Form::Form('OnlineUserForm');
			$this->link_css('style/active_users.css');
			CGlobal::$website_title='Thành viên online';
			if(isset($_GET['kick_out']) && User::is_admin()){
				 $session_id = Url::get('kick_out');
				 if($session_id){
				 	DB::delete(_SESS_TABLE,'session_id="'.$session_id.'"');
				 }
				 
				 Url::redirect_current();
			}
		}
	}
	
	function draw(){
		AZLib::getCats();
		
		global $display;
		$this->beginForm();
		$page_arr=array();
		
		$page_arr['home']				='Trang chủ';
		$page_arr['list_detail']		='Danh sách sản phẩm';
		$page_arr['profile']			='Profile thành viên';
		$page_arr['sign_in']			='Đăng nhập';
		
		$page_arr['item_detail']		='Chi tiết sản phẩm';
		$page_arr['online']				='Thành viên online';
		$page_arr['message']			='Tin nhắn cá nhân';
		$page_arr['post_item']			='Đăng sản phẩm';
		$page_arr['personal']			='Trang cá nhân';
		
		$page_arr['edit_page']			='Cấu hình page';
		$page_arr['admin']				='Trang quản trị';
		$page_arr['user']				='Quản lý thành viên';
		$page_arr['manage_item']		='Quản trị Tin';
		$page_arr['manage_image']		='Quản trị Ảnh';
		$page_arr['manage_comment']		='Quản Comment';
		$page_arr['manage_comment_user']='Quản trị Lưu bút';
		$page_arr['manage_bad_content']	='Quản trị tin xấu';
		$page_arr['manage_badword']		='Quản trị Từ xấu';
		$page_arr['partner']			='Quản trị Đối tác, ADV';
		$page_arr['send_multi']			='Gửi tin nhắn cho tất cả Thành viên';
		$page_arr['page']				='Quản trị Page';
		$page_arr['module']				='Quản trị Modules';
		$page_arr['shop']				='Trang cá nhân';
		$page_arr['sph_search ']		='Tìm kiếm';

		
		$not_permit_view=array(
								'edit_page',
								'admin',
								'user',
								'manage_item',
								'manage_comment',
								'manage_image',
								'manage_comment',
								'manage_comment_user',
								'manage_bad_content',
								'manage_badword',
								'partner',
								'send_multi',
								'page',
								'module',
								);
								
		$cond='';
		
		$item_type	=Url::get('item_type');
		$item_id	=Url::get('item_id');
		
		$o_name	=Url::get('o_name');
		$o_id	=(int)Url::get('o_id');
		
		$where='site';
		if($item_type&&$item_id&&in_array($item_type,array(1,2,3))){
			$cond=' AND page="ItemDetail" AND item_id='.$item_id.' AND item_type='.$item_type;
				
			if($item_type==3){
				$where='<font color=red>Sản phẩm đấu giá</font>';
			}
			elseif($item_type==1)
				$where='<font color=red>Giao dịch mua</font>';
			else
				$where='<font color=red>Sản phẩm  bán</font>';
		}
		
		if($o_name!=''){
			$str_search = str_replace( "'" , '"', $o_name );
			$str_search = str_replace( "&#39;" , '"', $str_search );
			$str_search = str_replace( "&quot;" , '"', $str_search );
		
			$cond.=' AND session_referer LIKE "%'.$str_search.'%"';
		}
		if($o_id){
			$cond.=' AND user_id ='.$o_id;
		}
		else{
			$o_id='';
		}
		
		$open_id_login = (int)Url::get('open_id_login',0);
		if($open_id_login){
			$display->add('open_id_login_check','checked="checked"');
			$cond.=" AND login_type = 1 ";
		}
		else{
			$display->add('open_id_login_check','');
		}
		
		
		$display->add('o_name',$o_name);
		$display->add('o_id',$o_id);

		$total_ss= DB::fetch('SELECT count(*) as total_ss FROM '._SESS_TABLE,'total_ss',0);
		$display->add('total_ss',$total_ss);
		
		$total= DB::fetch('SELECT count(*) as total FROM '._SESS_TABLE.' WHERE session_expires >'.( TIME_NOW - 900 ).' '.$cond);
		if($total)
			$total=(int)$total['total'];
		else	
			$total=0;
		
		$sql=	'SELECT count(total) as total FROM ( SELECT count(user_id) as total FROM '._SESS_TABLE.' WHERE session_expires >'.( TIME_NOW - 900 ).' AND user_id !=0 '.$cond.' GROUP BY user_id) as t';
		
		$total_mem= DB::fetch($sql,'total');
		
		$paging = AZPagging::paging($limit,$total_mem,25,10,'page_view');
		
		DB::query('SELECT session_id, session_expires as time, session_ip as ip, user_id, user_name, session_referer, page, category_id, item_type, item_id, open_id FROM '._SESS_TABLE.' WHERE session_expires >'.( TIME_NOW - 900 ).' AND user_id>0 '.$cond.' GROUP BY user_id  ORDER BY time DESC'.$limit);								
		$user_onlines=array();

		$i=1;
		while ($item = DB::fetch_row()){
			//$item['viewing_time'] = AZLib::duration(TIME_NOW-$item['time']);
			
			if(date('d',TIME_NOW)!=date('d',$item['time']))
				$item['viewing_time'] = date('H:i d/m/y',$item['time']);
			else
				$item['viewing_time'] = date('H:i',$item['time']);
			
			$item['user_name']	=	stripslashes($item['user_name']);
			$item['title']		=	'Thành viên';
			$item['link']		= 	WEB_DIR.$item['user_name'];
			
			if (isset($page_arr[$item['page']]) && (User::is_admin() || !in_array($item['page'],$not_permit_view))){
				$detail='';
				if($item['page']=='list_detail' && isset(CGlobal::$allCategories[$item['category_id']])){
					$detail=' - '.stripslashes(CGlobal::$allCategories[$item['category_id']]['name']);
					
					$parent_id=CGlobal::$allCategories[$item['category_id']]['parent_id'];
					if($parent_id && isset(CGlobal::$allCategories[$parent_id])){
						$detail=' - '.stripslashes(CGlobal::$allCategories[$parent_id]['name']).$detail;
					}
				}
					
				$item['viewing_page_name']=$page_arr[$item['page']].$detail;
			}
			elseif ($item['page'] && (User::is_admin() || !in_array($item['page'],$not_permit_view))){
				$item['viewing_page_name']=$item['page'];
			}
			else{
				$item['viewing_page_name']='enbac.com';
			}
			
			if(!$item['session_referer'] || (!User::is_admin() && in_array($item['page'],$not_permit_view))){
					$item['viewing_page_url']	=	WEB_ROOT;
			}
			else
				$item['viewing_page_url']=WEB_ROOT.stripslashes($item['session_referer']);
			
			if(User::is_admin() && $item['ip']){
				$ip_arr=explode('::',$item['ip']);
				$item['ip']='S_IP: <b>'.$ip_arr[0].'</b>';
				$item['ip'].=' - C_IP: <b>'.$ip_arr[1].'</b>';
			}
			else{
				$item['ip']='';
			}
			if(User::is_admin()){
				$item['del_s']='<a href="'.Url::build_current(array('kick_out'=>$item['session_id'])).'">Kick Out</a>';
			}
			else{
				$item['del_s']='';
			}
			$user_onlines[$i++]=$item;
		}
		
		$display->add('where',$where);
		$display->add('total',$total);
		$display->add('total_mem',$total_mem);
		$display->add('user_onlines',$user_onlines);
		$display->add('paging',$paging);
		
		$total_guest=0;
		$paging_guest='';
		$guest_onlines=array();
		
		if(User::is_admin()){
			$sql=	'SELECT  count(*) as total FROM 	'._SESS_TABLE.' WHERE session_expires >'.( TIME_NOW - 900 ).' AND user_id=0 '.$cond.' ';
			
			$total_guest= DB::fetch($sql,'total');
			$paging_guest = AZPagging::paging($limit,$total_guest,25,10,'guest_view');
			
			DB::query('SELECT session_id, session_expires as time, session_ip as ip, session_referer, page, category_id, item_type, item_id FROM '._SESS_TABLE.' WHERE session_expires >'.( TIME_NOW - 900 ).' AND user_id=0 '.$cond.' ORDER BY time DESC'.$limit);								
	
			$i=1;
			while ($item = DB::fetch_row()){
				//$item['viewing_time'] = AZLib::duration(TIME_NOW-$item['time']);
				if(date('d',TIME_NOW)!=date('d',$item['time']))
					$item['viewing_time'] = date('H:i d/m/y',$item['time']);
				else
					$item['viewing_time'] = date('H:i',$item['time']);
					
				$item['user_name']		  ='<font color="green">guest</font>';
				$item['viewing_page_url'] =WEB_ROOT;
				$item['link']			  =WEB_ROOT;
				
				if (isset($page_arr[$item['page']]) && (User::is_admin() || !in_array($item['page'],$not_permit_view))){
					$detail='';
					if($item['page']=='list_detail' && isset(CGlobal::$allCategories[$item['category_id']])){
						$detail=' - '.stripslashes(CGlobal::$allCategories[$item['category_id']]['name']);
						
						$parent_id=CGlobal::$allCategories[$item['category_id']]['parent_id'];
						if($parent_id && isset(CGlobal::$allCategories[$parent_id])){
							$detail=' - '.stripslashes(CGlobal::$allCategories[$parent_id]['name']).$detail;
						}
					}
						
					$item['viewing_page_name']=$page_arr[$item['page']].$detail;
				}
				elseif ($item['page'] && (User::is_admin() || !in_array($item['page'],$not_permit_view))){
					$item['viewing_page_name']=$item['page'];
				}
				else{
					$item['viewing_page_name']='enbac.com';
				}
				
				if(!$item['session_referer'] || (!User::is_admin() && in_array($item['page'],$not_permit_view))){
					$item['viewing_page_url']=WEB_ROOT;
				 }
				 else{
				 	$item['viewing_page_url']=WEB_ROOT.stripslashes($item['session_referer']);
				 }
				
				if(User::is_admin() && $item['ip']){
					$ip_arr=explode('::',$item['ip']);
					$item['ip']='S_IP: <b>'.$ip_arr[0].'</b>';
					$item['ip'].=' - C_IP: <b>'.$ip_arr[1].'</b>';
				}
				else{
					$item['ip']='';
				}
				if(User::is_admin()){
					$item['del_s']='<a href="'.Url::build_current(array('kick_out'=>$item['session_id'])).'">Kick Out</a>';
				}
				else{
					$item['del_s']='';
				}
				$guest_onlines[$i++]=$item;
			}
		}
		
		$display->add('total_guest',$total_guest);
		$display->add('guest_onlines',$guest_onlines);
		$display->add('paging_guest',$paging_guest);
		
		$display->output('OnlineUser');
		$this->endForm();
	}
}
?>